package com.ruoyi.web.controller.emp;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.EmpNormAttribute;
import com.ruoyi.system.domain.EmpNormSystem;
import com.ruoyi.system.service.IEmpNormAttributeService;
import com.ruoyi.system.service.IEmpNormSystemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 指标体系Controller
 *
 * @author zsl
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/system/system")
@Slf4j
public class EmpNormSystemController extends BaseController {
    @Autowired
    private IEmpNormSystemService empNormSystemService;

    @Autowired
    private IEmpNormAttributeService empNormAttributeService;

    /**
     * 查询指标体系列表
     */
    @PreAuthorize("@ss.hasPermi('system:system:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmpNormSystem empNormSystem) {
        startPage();
        List<EmpNormSystem> list = empNormSystemService.selectEmpNormSystemList(empNormSystem);
        return getDataTable(list);
    }

    /**
     * 导出指标体系列表
     */
    @PreAuthorize("@ss.hasPermi('system:system:export')")
    @Log(title = "指标体系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpNormSystem empNormSystem) {
        List<EmpNormSystem> list = empNormSystemService.selectEmpNormSystemList(empNormSystem);
        log.info("导出指标体系列表:{}", JSON.toJSONString(list));
        ExcelUtil<EmpNormSystem> util = new ExcelUtil<>(EmpNormSystem.class);
        util.exportExcel(response, list, "指标体系数据");
    }

    /**
     * 获取指标体系详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:system:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(empNormSystemService.selectEmpNormSystemById(id));
    }

    /**
     * 新增指标体系
     */
    @PreAuthorize("@ss.hasPermi('system:system:add')")
    @Log(title = "指标体系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpNormSystem empNormSystem) {
        empNormSystem.setCreateBy(getUsername());
        return toAjax(empNormSystemService.insertEmpNormSystem(empNormSystem));
    }

    /**
     * 修改指标体系
     */
    @PreAuthorize("@ss.hasPermi('system:system:edit')")
    @Log(title = "指标体系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpNormSystem empNormSystem) {
        empNormSystem.setUpdateBy(getUsername());
        return toAjax(empNormSystemService.updateEmpNormSystem(empNormSystem));
    }

    /**
     * 删除指标体系
     */
    @PreAuthorize("@ss.hasPermi('system:system:remove')")
    @Log(title = "指标体系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(empNormSystemService.deleteEmpNormSystemByIds(ids));
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('system:system:list')")
    @GetMapping("/attributeTree")
    public AjaxResult attributeTree(EmpNormAttribute attribute) {
        List<TreeSelect> treeSelects = empNormAttributeService.selectAttributeTreeList(attribute);
        log.info("treeSelects:{}", treeSelects.size());
        log.info("treeSelects:{}", JSON.toJSONString(treeSelects));
        return success(treeSelects);
    }

}
